<?php
	include dirname(__FILE__).'/../bootstrap/unit.php';

	new sfDatabaseManager($configuration);
//	Doctrine_Core::loadData(sfConfig::get('sf_data_dir').'/fixtures');

	$t = new lime_test(43, new lime_output_color());


	$t->diag("Setting model classes");


	$settingTable=Doctrine::getTable("Setting");
	$settingInstance=$settingTable->getLast();
	if (empty($settingInstance)){
		$settingInstance=new Setting();
		$settingInstance->save();
	}


/*
    $modelTable=Doctrine::getTable("Entry");
    $modelInstance=$modelTable->find(1);
*/

	// check Doctrine model methods
	$t->diag("Setting model classes - Doctrine standard methods");
	$modelId=$settingInstance->getId();
	$t->cmp_ok(intval($modelId), "==", $modelId, "getId() returns integer");

	$t->diag("Setting model classes - Doctrine Expando set/get methods");
	$settingInstance["test.set"]="test";
	$t->is($settingInstance["test.set"], "test", "Expando field returns previously set value");

	$settingInstance["TesT.Setv123"]="TesT.Set.123";
	$t->is($settingInstance["TesT.Setv123"], "TesT.Set.123", "Expando field returns previously set value with random case name");

	$settingInstance["test.setv123"]="TesT.Set.123";
	$t->is($settingInstance["test.setv123"], "TesT.Set.123", "Expando field returns previously set value with previously used name in all-lower case");

	$t->diag("Setting model classes - Doctrine Expando methods - fromArray/toArray");
	$settingInstance->fromArray(
		array(
			"test.arraykeyv1" => "test.array.val.1",
			"test.ArrayKeyv2" => "Test.Array.Val.2",
			"test.ArrayKeyv3" => "test.array.val.3",
			"test.arraykeyv3" => "test.array.val.4",//not an error, value must differ from test.Array.Key.3 to check key case lowering
		)
	);
	$t->is($settingInstance["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
	$t->is($settingInstance["test.ArrayKeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name");
	$t->is($settingInstance["test.ArrayKeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");
	$t->is($settingInstance["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


	$settingsArray=$settingInstance->toArray();
	$t->is($settingsArray["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
	$t->ok(!array_key_exists("test.ArrayKeyv2", $settingsArray), "Expando field doesnt return previously fromArray set value with random case name");
	$t->ok($settingsArray["test.arraykeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name as all lower key");
	$t->ok(!array_key_exists("test.ArrayKeyv3", $settingsArray), "Expando field doesnt return previously fromArray set value with random case name");
	$t->is($settingsArray["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


	$t->diag('Saving Setting instance to database');
	$settingInstance->save();

	$t->diag('Loading Setting instance from database');
	$settingInstance=$settingTable->find($modelId);

	$t->is($settingInstance["test.set"], "test", "Expando field returns previously saved value");
	$t->is($settingInstance["TesT.Setv123"], "TesT.Set.123", "Expando field returns previously saved value with random case name");
	$t->is($settingInstance["test.setv123"], "TesT.Set.123", "Expando field returns previously saved value with previously used name in all-lower case");
	$t->is($settingInstance["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray saved value");
	$t->is($settingInstance["test.ArrayKeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray saved value with random case name");
	$t->is($settingInstance["test.ArrayKeyv3"], "test.array.val.4", "Expando field returns previously fromArray saved value with random case previously used name");
	$t->is($settingInstance["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray saved value with random case previously used name");

	$t->diag("Setting model classes - Doctrine Expando methods - fromArray/toArray");
	$settingInstance->fromArray(
		array(
			"test.arraykeyv1" => "test.array.val.1",
			"test.ArrayKeyv2" => "Test.Array.Val.2",
			"test.ArrayKeyv3" => "test.array.val.3",
			"test.arraykeyv3" => "test.array.val.4",//not an error, value must differ from test.Array.Key.3 to check key case lowering
		)
	);
	$t->is($settingInstance["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
	$t->is($settingInstance["test.ArrayKeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name");
	$t->is($settingInstance["test.ArrayKeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");
	$t->is($settingInstance["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


	$settingsArray=$settingInstance->toArray();
	$t->is($settingsArray["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
	$t->ok(!array_key_exists("test.ArrayKeyv2", $settingsArray), "Expando field doesnt return previously fromArray set value with random case name");
	$t->ok($settingsArray["test.arraykeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name as all lower key");
	$t->ok(!array_key_exists("test.ArrayKeyv3", $settingsArray), "Expando field doesnt return previously fromArray set value with random case name");
	$t->is($settingsArray["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");




	$t->diag("Berta Settings Singleton");

	$settingsInstance=Settings::getInstance();

	$t->diag("ArrayAccess interface");
	$t->is($settingsInstance["test.set"], "test", "Expando field returns previously set value");
	$t->is($settingsInstance["TesT.Setv123"], "TesT.Set.123", "Expando field returns previously set value with random case name");
	$t->is($settingsInstance["test.setv123"], "TesT.Set.123", "Expando field returns previously set value with previously used name in all-lower case");
	$t->is($settingsInstance["test.arraykeyv1"], "test.array.val.1", "Expando field returns previously fromArray set value");
	$t->is($settingsInstance["test.ArrayKeyv2"], "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name");
	$t->is($settingsInstance["test.ArrayKeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");
	$t->is($settingsInstance["test.arraykeyv3"], "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");

	$t->diag("TwigObject interface");
	$t->is($settingsInstance->test->set, "test", "Expando field returns previously set value");
	$t->is($settingsInstance->TesT->Setv123, "TesT.Set.123", "Expando field returns previously set value with random case name");
	$t->is($settingsInstance->test->setv123, "TesT.Set.123", "Expando field returns previously set value with previously used name in all-lower case");
	$t->is($settingsInstance->test->arraykeyv1, "test.array.val.1", "Expando field returns previously fromArray set value");
	$t->is($settingsInstance->test->ArrayKeyv2, "Test.Array.Val.2", "Expando field returns previously fromArray set value with random case name");
	$t->is($settingsInstance->test->ArrayKeyv3, "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");
	$t->is($settingsInstance->test->arraykeyv3, "test.array.val.4", "Expando field returns previously fromArray set value with random case previously used name");


